package com.example.web;

import com.example.dao.UserRepository;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.queue.QueuesHolder;
import com.example.queue.TestQueue;
import com.example.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @Title: spring-boot-druid
 * @Package com.example
 * @Date 2018/5/7下午1:52
 * @Author PX ye
 * @E-mail yepx2013@gmail.com
 * @Company http://www.yiautos.com
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/user")
@EnableAutoConfiguration
public class UserController {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRepository userRepository;

    @Autowired
    private StringRedisTemplate template;


    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    String test(){
        return "test";
    }

    @RequestMapping("/save")
    Long save(@RequestParam String name, @RequestParam Integer age){
        User user = new User(name, age);
        return userRepository.save(user).getId();
    }

    @RequestMapping("/findById")
    User findById(@RequestParam Long id){
        return userRepository.findById(id).get();
    }

    @RequestMapping("/findByName")
    User findByName(@RequestParam String name){

        //通常使用Specification 的匿名内部类，
        Specification<User> specification = new Specification<User>(){

            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.like(root.get("name").as(String.class), "%"+ name +"%");
            }
        };

        return userRepository.findOne(specification).get();
    }

    @RequestMapping("/findByPage")
    Page<User> findByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){

        // jpa
        Pageable pageable = new PageRequest(pageNum, pageSize, Sort.Direction.ASC, "id");
        return userRepository.findAll(pageable);
    }

    @RequestMapping("/findByPage2")
    com.github.pagehelper.Page findByPage2(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.findAll();
        System.out.println("Total: " + ((com.github.pagehelper.Page) userList).getTotal());
        for (User country : userList) {
            System.out.println("Country Name: " + country.getName());
        }

        return ((com.github.pagehelper.Page) userList);
    }

    @RequestMapping("/testQueue")
    void testQueue(@RequestParam String name){
        //TODO java 阻塞队列test
        QueuesHolder.getQueue(TestQueue.class.getName()).offer(name);
    }

    @RequestMapping("/findAddressAndCarById")
    User findAddressAndCarById(@RequestParam Long id){
        return userMapper.findUserWithAddressAndCar(id);
    }

    @RequestMapping("/setRedisValue")
    public String setRedisValue(@RequestParam String key, @RequestParam String value){

        template.opsForValue().append(key, value);
        return "使用redis缓存保存数据成功";
    }

    @RequestMapping("/delRedisValue")
    public String delRedisValue(@RequestParam String key){

        template.delete(key);
        return "key【" + key + "】已经删除。";
    }

    @RequestMapping("getUserByRedis")
    public User getUserByRedis(@RequestParam Long id){
        User user = userService.getUser(id);
        System.out.println(user.getName());
        return user;
    }

    @RequestMapping("delUserByRedis")
    public void delUserByRedis(@RequestParam Long id){
         userService.deleteUser(id);
    }

}
